import { message } from 'antd';
import {
  getEmpList,
  locked,
  getEmpDetail,
  getAssList,
  getServiceList,
  getAddTaskList,
  getReduceTaskList,
  getAdjustTaskList,
  getEmpInsSerInfo,
  getEmpInsRefundInfo,
  getWageInfo,
  getRespUserList,
} from '@/services/empsearch';

export default {
  state: {
    formFieldsValue: {}, // 表单域
    expandForm: false, // 展开收起
    data: {
      // 列表数据
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    addTaskList: {
      // 增员任务列表数据
      list: [],
    },
    reduceTaskList: {
      // 减员任务列表数据
      list: [],
    },
    adjustTaskList: {
      // 调整任务列表数据
      list: [],
    },
    empInsSerInfoList: {
      // 社保服务办理列表数据
      list: [],
    },
    empInsRefundInfoList: {
      // 返款信息列表数据
      list: [],
    },
    wageDataList: {
      // 代发工资列表数据
      list: [],
    },
    valuesArr: [
      'empName',
      'idCode',
      'custId',
      'sendStatus',
      'sendCorp',
      'sendTime',
      'redEmpTime',
      'primId',
    ],
    respUserList: [],
  },
  effects: {
    // 获取列表
    *fetchList({ payload }, { call, put }) {
      const response = yield call(getEmpList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'getList',
        payload: {
          data: [],
          currentPage,
          pageSize,
        },
      });
      if (response.success) {
        yield put({
          type: 'getList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },
    // 锁定/解锁
    *locked({ payload }, { call }) {
      const response = yield call(locked, payload);
      return response;
    },
    // 获取员工查询的详情
    *getEmpDetail({ payload }, { call }) {
      const response = yield call(getEmpDetail, payload);
      return response;
    },
    // 获取员工险种的列表数据
    *getAssList({ payload }, { call }) {
      const response = yield call(getAssList, payload);
      return response;
    },
    // 获取员工服务费的列表数据
    *getServiceList({ payload }, { call }) {
      const response = yield call(getServiceList, payload);
      return response;
    },
    // 获取增员任务列表
    *getAddTaskList({ payload }, { call, put }) {
      const response = yield call(getAddTaskList, payload);
      yield put({
        type: 'getAddTask',
        payload: {
          data: {},
        },
      });
      if (response.success) {
        yield put({
          type: 'getAddTask',
          payload: {
            data: response.data,
          },
        });
      } else {
        message.warning(response.message || '获取增员任务列表数据错误：服务器端错误');
      }
    },
    // 获取减员任务列表
    *getReduceTaskList({ payload }, { call, put }) {
      const response = yield call(getReduceTaskList, payload);
      yield put({
        type: 'getReduceTask',
        payload: {
          data: {},
        },
      });
      if (response.success) {
        yield put({
          type: 'getReduceTask',
          payload: {
            data: response.data,
          },
        });
      } else {
        message.warning(response.message || '获取减员任务列表数据错误：服务器端错误');
      }
    },
    // 获取调整任务列表
    *getAdjustTaskList({ payload }, { call, put }) {
      const response = yield call(getAdjustTaskList, payload);
      yield put({
        type: 'getAdjustTask',
        payload: {
          data: {},
        },
      });
      if (response.success) {
        yield put({
          type: 'getAdjustTask',
          payload: {
            data: response.data,
          },
        });
      } else {
        message.warning(response.message || '获取调整任务列表数据错误：服务器端错误');
      }
    },
    // 获取社保服务办理数据
    *getEmpInsSerInfoData({ payload }, { call, put }) {
      const response = yield call(getEmpInsSerInfo, payload);
      yield put({
        type: 'empInsSerInfo',
        payload: {
          data: {},
        },
      });
      if (response.success) {
        yield put({
          type: 'empInsSerInfo',
          payload: {
            data: response.data,
          },
        });
      } else {
        message.warning(response.message || '获取社保服务办理列表数据错误：服务器端错误');
      }
    },
    // 获取返款信息数据
    *getEmpInsRefundInfoData({ payload }, { call, put }) {
      const response = yield call(getEmpInsRefundInfo, payload);
      yield put({
        type: 'empInsRefundInfo',
        payload: {
          data: {},
        },
      });
      if (response.success) {
        yield put({
          type: 'empInsRefundInfo',
          payload: {
            data: response.data,
          },
        });
      } else {
        message.warning(response.message || '获取返款信息列表数据错误：服务器端错误');
      }
    },
    // 获取代发工资数据
    *getWageInfoData({ payload }, { call, put }) {
      const response = yield call(getWageInfo, payload);
      yield put({
        type: 'getWageInfoList',
        payload: {
          data: {},
        },
      });
      if (response.success) {
        yield put({
          type: 'getWageInfoList',
          payload: {
            data: response.data,
          },
        });
      } else {
        message.warning(response.message || '获取代发工资列表数据错误：服务器端错误');
      }
    },
    // 获取所属客服列表
    *fetchRespUserList({ payload }, { call, put }) {
      const response = yield call(getRespUserList, payload);
      yield put({
        type: 'updateModelData',
        payload: {
          respUserList: [],
        },
      });
      if (response.success && response.data) {
        yield put({
          type: 'updateModelData',
          payload: {
            respUserList: Array.isArray(response.data) ? response.data : [],
          },
        });
      } else {
        message.warning(response.message || '获取所属客服数据：服务端错误');
      }
    },
  },

  reducers: {
    // 获取列表
    getList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        data: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },
    // 获取增员任务列表数据
    getAddTask(
      state,
      {
        payload: { data },
      }
    ) {
      const list = Array.isArray(data) ? data : [];
      return {
        ...state,
        addTaskList: {
          list,
        },
      };
    },
    // 获取减员任务列表数据
    getReduceTask(
      state,
      {
        payload: { data },
      }
    ) {
      const list = Array.isArray(data) ? data : [];
      return {
        ...state,
        reduceTaskList: {
          list,
        },
      };
    },
    // 获取调整任务列表数据
    getAdjustTask(
      state,
      {
        payload: { data },
      }
    ) {
      const list = Array.isArray(data) ? data : [];
      return {
        ...state,
        adjustTaskList: {
          list,
        },
      };
    },
    // 获取社保服务办理列表数据
    empInsSerInfo(
      state,
      {
        payload: { data },
      }
    ) {
      const list = Array.isArray(data) ? data : [];
      return {
        ...state,
        empInsSerInfoList: {
          list,
        },
      };
    },
    // 获取返款信息列表数据
    empInsRefundInfo(
      state,
      {
        payload: { data },
      }
    ) {
      const list = Array.isArray(data) ? data : [];
      return {
        ...state,
        empInsRefundInfoList: {
          list,
        },
      };
    },
    // 获取代发工资列表数据
    getWageInfoList(
      state,
      {
        payload: { data },
      }
    ) {
      const list = Array.isArray(data) ? data : [];
      return {
        ...state,
        wageDataList: {
          list,
        },
      };
    },
    // 设置修改当前state数据
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    // 设置查询时保存的值
    updateTabListQuery(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
